<template>
  <div>
    <div style="margin-top: 2%;text-align:center;">
      <el-button @click="addSubType()" style="font-size: 20px" type="primary">点击此处新增测验类型</el-button>
    </div>

    <template style="margin-top: 2%">
      <el-table :data="pageInfo.list" style="width: 100%">
        <el-table-column label="" width="360">
        </el-table-column>
        <el-table-column label="" width="300" prop="subTypeId" sortable>
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{scope.row.subTypeId}}</span>
          </template>
        </el-table-column>
        <el-table-column label="测验类型" width="600" prop="subTypeName" sortable>
          <template slot-scope="scope">
            <span>{{scope.row.subTypeName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                size="small"
                style="background-color: deepskyblue;font-size: 16px"
                @click="handleEdit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-bottom: 1%;"></div>
      <el-pagination
          style="text-align: center;"
          background
          layout="total,prev, pager, next"
          :total="pageInfo.total"
          :page-size="pageInfo.pageSize"
          @current-change="handleCurrentChange">
      </el-pagination>
    </template>

    <el-drawer title="新增测验类型" :visible.sync="add" :direction="addDirection">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="30%">
        <el-form-item label="测验类型" prop="subTypeName">
          <el-input v-model="ruleForm.subTypeName" style="width: 60%"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">立即新增</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>

    <el-drawer title="修改测验类型" :visible.sync="modify" :direction="modifyDirection">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="30%">
        <el-form-item label="测验类型" prop="subTypeName">
          <el-input v-model="ruleForm.subTypeName" style="width: 60%"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">立即修改</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
export default {
  data(){
    return {
      add: false,
      addDirection: 'btt',
      modify: false,
      modifyDirection: 'btt',
      currPage: 1,
      pageInfo:{},
      ruleForm:{
        subTypeId: '',
        subTypeName: '',
      },
      rules: {
        subTypeName: [
          { required: true, message: '请输入测验类型', trigger: 'blur' },
          { min: 2, max: 30, message: '请输入 2 到 30 个字符!', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getData();
  },
  methods:{
    submitForm(formName){ // 新增/修改测验类型
      this.$refs[formName].validate((valid) => {//校验格式
        if (valid) {//格式校验通过
          let path = 'http://localhost:8081/subScore/updateType';
          this.$ajax.post(path,this.ruleForm).then(res=>{
            if(res.data.status === 0){
              this.$message.success(res.data.message);
              this.add = false;
              this.modify = false;
              this.getData();
            }
          })
        }
      });
    },
    addSubType(){
      this.ruleForm.subTypeId = '';
      this.ruleForm.subTypeName = '';
      this.add = true;
    },
    handleEdit(row){
      this.ruleForm.subTypeId = row.subTypeId;
      this.ruleForm.subTypeName = row.subTypeName;
      this.modify = true;
    },
    handleCurrentChange(val){
      this.currPage = val;
      this.getData();
    },
    getData(){
      let path = `http://localhost:8081/subScore/getSubType/${this.currPage}`;
      this.$ajax.post(path).then(res=>{
        this.pageInfo = res.data;
      })
    }
  }
}
</script>

<style scoped>
span{
  font-size: 18px;
}
</style>